<template>
  <div class="register-container">
    <el-card class="register-box">
      <h2 class="register-title">用户注册</h2>
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" show-password />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" :loading="loading">注册</el-button>
          <el-button @click="goToLogin">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {register} from '@/api/auth.js'

const router = useRouter()
const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: ''
})

const loading = ref(false)

const validatePassword = (rule, value, callback) => {
  if (value !== registerForm.value.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 20, message: '用户名长度在4到20个字符', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9_]+$/,
      message: '用户名只能包含字母、数字和下划线',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6到20个字符', trigger: 'blur' },
    {
      pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,20}$/,
      message: '密码必须包含大小写字母和数字',
      trigger: 'blur'
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePassword, trigger: 'blur' }
  ]
}

const registerFormRef = ref()

const handleRegister = async () => {
  try {
    // 调用注册接口
    const registerRequest = {
      username: registerForm.value.username,
      password: registerForm.value.password
    }
    register(registerRequest).then((res) => {
      if (res && res.code === 200) {
        ElMessage.success(response.message || '注册成功，请登录')
        router.push('/login')
      } else {
        ElMessage.error(response?.message || '注册失败，请稍后重试')
      }
    })

    // 处理响应

  } catch (error) {
    console.error('注册错误:', error)
    ElMessage.error(
      error.response?.data?.message ||
      error.message ||
      '注册失败，请检查网络连接'
    )
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(120deg, #fdfbfb 0%, #ebedee 100%);
}

.register-box {
  width: 500px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
  color: #409eff;
  font-weight: 500;
  font-size: 24px;
}

.el-form-item:last-child {
  margin-bottom: 0;
  display: flex;
  justify-content: center;
}

.el-button {
  margin: 0 10px;
  width: 120px;
}

.el-input {
  width: 100%;
}
</style>
